#include "TPM_HAL_QE.h"

void vfnTPM_ClockSelect (uint8 u8timer, uint8 u8source)
{
    if (u8timer == 1)
    {
        TPM1SC_CLKSx = u8source;
    }
    else if (u8timer == 2)
    {
        TPM2SC_CLKSx = u8source;
    }
    else
    {
        TPM3SC_CLKSx = u8source;
    }
}

void vfnTPM_PrescalerSelect (uint8 u8timer, uint8 u8prescaler)
{
    if (u8timer == 1)
    {
        TPM1SC_PS = u8prescaler;
    }
    else if (u8timer == 2)
    {
        TPM2SC_PS = u8prescaler;
    }
    else
    {
        TPM3SC_PS = u8prescaler;
    }    
}

void vfnTPM_OverflowInterrupts (uint8 u8timer, uint8 u8config)
{
    if (u8timer == 1)
    {
        TPM1SC_TOIE = u8config;
    }
    else if (u8timer == 2)
    {
        TPM2SC_TOIE = u8config;
    }
    else
    {
        TPM3SC_TOIE = u8config;
    }    
}

void vfnTPM_SetModulo (uint8 u8timer, uint16 u16modulo)
{
    if (u8timer == 1)
    {
        TPM1MOD = u16modulo;
    }
    else if (u8timer == 2)
    {
        TPM2MOD = u16modulo;
    }
    else
    {
        TPM3MOD = u16modulo;
    }    
}

void vfnTPM_SetChannelValue (uint8 u8timer, uint8 u8channel, uint16 u16value)
{
    if (u8timer == 1)
    {
        if (TPM1SC_CPWMS)
        {
            u16value &= 0x7FFF;
        }
        
        if (u8channel == 0)
        {
            TPM1C0V = u16value;
        }
        else if (u8channel == 1)
        {
            TPM1C1V = u16value;
        }
        else
        {
            TPM1C2V = u16value;
        }
    }
    else if (u8timer == 2)
    {
        if (TPM2SC_CPWMS)
        {
            u16value &= 0x7FFF;
        }
    
        if (u8channel == 0)
        {
            TPM2C0V = u16value;
        }
        else if (u8channel == 1)
        {
            TPM2C1V = u16value;
        }
        else
        {
            TPM2C2V = u16value;
        }
    }
    else
    {
        if (TPM3SC_CPWMS)
        {
            u16value &= 0x7FFF;
        }
        
        if (u8channel == 0)
        {
            TPM3C0V = u16value;
        }
        else if (u8channel == 1)
        {
            TPM3C1V = u16value;
        }
        else if (u8channel == 2)
        {
            TPM3C2V = u16value;
        }
        else if (u8channel == 3)
        {
            TPM3C3V = u16value;
        }
        else if (u8channel == 4)
        {
            TPM3C4V = u16value;
        }
        else
        {
            TPM3C5V = u16value;
        }
    }
}

void vfnTPM_ConfigureCAPWM (uint8 u8timer, uint8 u8channel, uint8 u8config)
{
    if (u8timer == 1)
    {
        TPM1SC_CPWMS = 1;
        
        if (u8channel == 0)
        {
            TPM1C0SC_ELS0x = u8config;
        }
        else if (u8channel == 1)
        {
            TPM1C1SC_ELS1x = u8config;
        }
        else
        {
            TPM1C2SC_ELS2x = u8config;
        }
    }
    else if (u8timer == 2)
    {
        TPM2SC_CPWMS = 1;
        
        if (u8channel == 0)
        {
            TPM2C0SC_ELS0x = u8config;
        }
        else if (u8channel == 1)
        {
            TPM2C1SC_ELS1x = u8config;
        }
        else
        {
            TPM2C2SC_ELS2x = u8config;
        }
    }
    else
    {
        TPM3SC_CPWMS = 1;
        
        if (u8channel == 0)
        {
            TPM3C0SC_ELS0x = u8config;
        }
        else if (u8channel == 1)
        {
            TPM3C1SC_ELS1x = u8config;
        }
        else if (u8channel == 2)
        {
            TPM3C2SC_ELS2x = u8config;
        }
        else if (u8channel == 3)
        {
            TPM3C3SC_ELS3x = u8config;
        }
        else if (u8channel == 4)
        {
            TPM3C4SC_ELS4x = u8config;
        }
        else
        {
            TPM3C5SC_ELS5x = u8config;
        }
    }
}

void vfnTPM_ConfigureEAPWM (uint8 u8timer, uint8 u8channel, uint8 u8config)
{
    if (u8timer == 1)
    {   
        if (u8channel == 0)
        { 
            TPM1C0SC_MS0B = 1;
            TPM1C0SC_ELS0x = u8config;
        }
        else if (u8channel == 1)
        {
            TPM1C1SC_MS1B = 1;
            TPM1C1SC_ELS1x = u8config;
        }
        else
        {
            TPM1C2SC_MS2B = 1;
            TPM1C2SC_ELS2x = u8config;
        }
    }
    else if (u8timer == 2)
    {
        if (u8channel == 0)
        {
            TPM2C0SC_MS0B = 1;
            TPM2C0SC_ELS0x = u8config;
        }
        else if (u8channel == 1)
        {
            TPM2C1SC_MS1B = 1;
            TPM2C1SC_ELS1x = u8config;
        }
        else
        {
            TPM2C2SC_MS2B = 1;
            TPM2C2SC_ELS2x = u8config;
        }
    }
    else
    {
        if (u8channel == 0)
        {
            TPM3C0SC_MS0B = 1;
            TPM3C0SC_ELS0x = u8config;
        }
        else if (u8channel == 1)
        {
            TPM3C1SC_MS1B = 1;
            TPM3C1SC_ELS1x = u8config;
        }
        else if (u8channel == 2)
        {
            TPM3C2SC_MS2B = 1;
            TPM3C2SC_ELS2x = u8config;
        }
        else if (u8channel == 3)
        {
            TPM3C3SC_MS3B = 1;
            TPM3C3SC_ELS3x = u8config;
        }
        else if (u8channel == 4)
        {
            TPM3C4SC_MS4B = 1;
            TPM3C4SC_ELS4x = u8config;
        }
        else
        {
            TPM3C5SC_MS5B = 1;
            TPM3C5SC_ELS5x = u8config;
        }
    }
}


uint16 u16TPM3_GetChannelValue (uint8 u8channel)
{
	if (u8channel == 0)
    {
        return TPM3C0V;
    }
    else if (u8channel == 1)
    {
        return TPM3C1V;
    }
    else if (u8channel == 2)
    {
        return TPM3C2V;
    }
    else if (u8channel == 3)
    {
        return TPM3C3V;
    }
    else if (u8channel == 4)
    {
        return TPM3C4V;
    }
    else
    {
        return TPM3C5V;
    }
}

void vfnTPM2_SoftwareOutputCompare (uint8 u8channel)
{
	if (u8channel == 0)
	{
		TPM2C0SC_MS0x = 0x01;
		TPM2C0SC_ELS0x = 0x00;
	}
	else if (u8channel == 1)
	{
		TPM2C1SC_MS1x = 0x01;
		TPM2C1SC_ELS1x = 0x00;
	}
	else if (u8channel == 2)
	{
		TPM2C2SC_MS2x = 0x01;
		TPM2C2SC_ELS2x = 0x00;
	}
}









